$(function () {
    $.ajax({
        url:"/authenticate_",
        type:"GET",
        contentType:"application/json;charset=utf-8",
        dataType:"json",
        success:function (response) {
            if(response.code===200){
                console.log(response);
                $(".login").hide();
                $(".signin").hide();
                $(".publish").show();
                $(".user").show();
                $(".avatar_url").attr("src",response.data.avatar_url);
                $(".username").text(response.data.username);
            }
        },
        error:function (request) {
            console.log(request);
        }
    });
    $("input[type='password'][data-eye]").each(function (i) {
        let $this = $(this);

        $this.wrap($("<div/>", {
            style: 'position:relative'
        }));
        $this.css({
            paddingRight: 60
        });
        $this.after($("<span/>", {
            html: '',
            class: 'glyphicon glyphicon-eye-open',
            id: 'passeye-toggle-' + i,
            style: 'position:absolute;right:10px;top:50%;transform:translate(0,-50%);padding: 2px 7px;font-size:15px;cursor:pointer;'
        }));
        $this.after($("<input/>", {
            type: 'hidden',
            id: 'passeye-' + i
        }));
        $this.on("keyup paste", function () {
            $("#passeye-" + i).val($(this).val());
        });
        $("#passeye-toggle-" + i).on("click", function () {
            if ($this.hasClass("show")) {
                $this.attr('type', 'password');
                $this.removeClass("show");
                $(this).removeClass("btn-outline-primary");
            } else {
                $this.attr('type', 'text');
                $this.val($("#passeye-" + i).val());
                $this.addClass("show");
                $(this).addClass("btn-outline-primary");
            }
        });
    });
    checkAddress();

});

function checkAccount() {
    var username = $("#username").val();
    var password = $("#password").val();
    var password_=$("#password_").val();
    if((password_!==password)){
        $(".tip").text("密码不一致！");
        $(".tip").css({"color":"red","font-size":"13px"});
        return false;
    }
    return true;
}

function checkAddress() {
    var check_ = location.href.split("?").pop();
    if(check_==="error"){
        $(".tip").text("用户名重复！");
    }else if(check_==="success"){
        $(".tip").css({"color":"green"}).text("注册成功，即将跳到首页...");
        setTimeout(function () {
            window.location.href="http://localhost/index.html";
        },1000);
    }

}